package com.personal.o2o.service.impl;

import com.personal.o2o.dao.ShopDao;
import com.personal.o2o.dto.ImageHolder;
import com.personal.o2o.dto.ShopExecution;
import com.personal.o2o.entity.Shop;
import com.personal.o2o.enums.ShopStateEnum;
import com.personal.o2o.exceptions.ShopOperationException;
import com.personal.o2o.service.ShopService;
import com.personal.o2o.util.ImageUtil;
import com.personal.o2o.util.PageCalculator;
import com.personal.o2o.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopDao shopDao;

    @Override
    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        // 提取行号
            int rowIndex = PageCalculator.calculateRowIndex(pageIndex,pageSize);
        // 获取数据
        List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);
        // 获取总数
        int count = shopDao.queryShopCount(shopCondition);
        ShopExecution se = new ShopExecution();
        if (shopList!=null) {
            se.setShopList(shopList);
            se.setCount(count);
        }else{
            se.setState(ShopStateEnum.INNER_ERROR.getState());
        }
        return se;
    }

    @Override
    public Shop getByShopId(long shopId) {
        return shopDao.queryByShopId(shopId);
    }

    @Override
    public ShopExecution modifyShop(Shop shop, ImageHolder thumbnali) throws ShopOperationException {
        try {
            if (shop == null || shop.getShopId() == null) {
                return new ShopExecution(ShopStateEnum.NULL_SHOP);
            } else {
                // 1.判断是否需要处理图片
                if (thumbnali.getImage() != null) {
                    Shop tempShop = shopDao.queryByShopId(shop.getShopId());
                    // 如果店铺图片不为空的
                    if (tempShop.getShopImg() != null && thumbnali.getImageName() != null && !"".equals(thumbnali.getImageName())) {
                        ImageUtil.deleteFileOrPath(tempShop.getShopImg());
                    }
                    // 重新添加店铺图片
                    addShopImg(shop, thumbnali);
                }
                // 2.更新店铺信息
                shop.setLastEditTime(new Date());
                int effectedNum = shopDao.updateShop(shop);
                if (effectedNum <= 0) {
                    return new ShopExecution(ShopStateEnum.INNER_ERROR);
                } else {
                    shop = shopDao.queryByShopId(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS, shop);
                }
            }
        }catch (Exception e) {
            throw new ShopOperationException("modifyShop error:"+e.getStackTrace());
        }
    }

    /**
     * 添加店铺实现方法
     * @param shop
     * @param thumbnali
     * @return
     */
    @Override
    @Transactional
//    public ShopExecution addShop(Shop shop, File shopImg) {
    public ShopExecution addShop(Shop shop, ImageHolder thumbnali) {
        // 判断店铺是否传入为空
        if (shop==null) {
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }
        try{
            //给店铺信息赋初始值
            shop.setEnableStatus(0);
            shop.setCreateTime(new Date());
            shop.setLastEditTime(new Date());
            //添加店铺信息
            int effectedNum = shopDao.insertShop(shop);
            if(effectedNum<=0){
                throw new ShopOperationException("店铺创建失败");
                // 当程序当且仅当抛出ShopOperationException或者继承ShopOperationException，事务才会得以终止并回滚。
            }else{
                if (thumbnali.getImage() !=null) {
                    // 店铺图片存储
                    try{
//                        addShopImg(shop,shopImgInputStream);
                        addShopImg(shop,thumbnali);
                    }catch (Exception e){
                        throw new ShopOperationException("addShopImg error:"+e.getMessage());
                    }
                    //更新店铺的图片地址
                    effectedNum = shopDao.updateShop(shop);
                    if (effectedNum<=0) {
                        throw new ShopOperationException("更新图片地址失败");
                    }
                }
            }

        }catch (Exception e){
            throw new ShopOperationException("addShop error:"+e.getMessage());
        }
        return new ShopExecution(ShopStateEnum.CHECK,shop);
    }

//    private void addShopImg(Shop shop, File shopImg) {
    private void addShopImg(Shop shop,ImageHolder ImageHolder) {
        // 获取店铺图片目录的相对值路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());
//        String shopImgAddr = ImageUtil.generateThumbnail(shopImg,dest);
        String shopImgAddr = ImageUtil.generateThumbnail(ImageHolder,dest);
        shop.setShopImg(shopImgAddr);
    }
}
